package com.bjb.vr.remote.ws.rabbitmq.handler.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bjb.vr.common.dto.SysConfig;
import com.bjb.vr.remote.ws.rabbitmq.constants.MessageActionConstants;
import com.bjb.vr.remote.ws.rabbitmq.entity.RabbitWebSocketMsg;
import com.bjb.vr.remote.ws.rabbitmq.handler.MessageHandler;
import com.bjb.vr.remote.ws.websocket.entity.Action;
import com.bjb.vr.remote.ws.websocket.entity.Command;
import com.bjb.vr.remote.ws.websocket.entity.RingCountParameter;
import com.bjb.vr.remote.ws.websocket.vo.Message;
import com.bjb.vr.remote.ws.websocket.websocket.WebSocketMsgHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import  com.bjb.vr.common.service.SysConfigService;

import java.util.List;

/**
 * @version 1.0.0
 * @description:
 * @author: caihaibo
 * @createDate: 2022/3/29
 */
@Slf4j
@Component
public class RingMessageHandler implements MessageHandler {

    @Autowired
    SysConfigService sysConfigService;

    @Override
    public void process(RabbitWebSocketMsg rabbitWebSocketMsg) {

        JSONObject jsonObject = JSONObject.parseObject(rabbitWebSocketMsg.getMsg());

        Message message = JSONObject.toJavaObject(jsonObject, Message.class);



        if(message.getAction().equals(MessageActionConstants.MESSAGE_ACTION_RING)){
            log.info("处理提醒信息");
            Action action = new Action();
            action.setAction(MessageActionConstants.MESSAGE_ACTION_RING);
            Command command = new Command<>();

            command.setUdid(message.getCommand().getUdid());
            command.setDevId(message.getCommand().getDevId());

            LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SysConfig::getTenantId,rabbitWebSocketMsg.getTenantId());
            SysConfig sysConfig =   sysConfigService.getOne(queryWrapper);
            int  ringCount = 5 ;
            if(sysConfig!=null ){
                ringCount = sysConfig.getCountOfRing();
            }
            RingCountParameter parameter = new RingCountParameter();
            parameter.setRingCount(ringCount);
            command.setParameter(parameter);
            action.setCommand(command);

            rabbitWebSocketMsg.setMsg(JSONObject.toJSONString(action));
            log.info("转发消息 " + rabbitWebSocketMsg.getMsg());
            WebSocketMsgHandler.sendSingleClient(rabbitWebSocketMsg,message);

        }

    }
}
